package com.etc.service.impl;

import com.etc.dao.LiveDao;
import com.etc.dao.TypeDao;
import com.etc.entity.*;
import com.etc.service.LiveService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LiveServiceImpl implements LiveService {
    
    @Autowired
    private LiveDao liveDao;

    /**
     * 根据ID查询直播间
     * @param roomId
     * @return
     */
    @Override
    public Room queryRoomById(String roomId) {
        return liveDao.queryRoomById(roomId);
    }
    
    /**
     * 查询所有直播间
     * @return
     */
    @Override
    public PageInfo<Room> queryRoom(String Anchor, String roomName,int pageNo,int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<Room> rooms = liveDao.queryRoom(Anchor, roomName);
        PageInfo<Room> pageInfo = new PageInfo<>(rooms);
        return pageInfo;
    }
    /**
     * 添加房间
     * @param room
     * @return
     */
    @Override
    public boolean addRoom(Room room) {
        return liveDao.addRoom(room)>0;
    }
    /**
     * 更新直播间信息
     * @param room
     * @return
     */
    @Override
    public boolean updateRoom(Room room) {
        return liveDao.updateRoom(room)>0;
    }
    /**
     * 删除房间
     * @param roomId
     * @return
     */
    @Override
    public boolean deleteRoom(String roomId) {
        return liveDao.deleteRoom(roomId)>0;
    }
    /**
     * 查询所有分类
     * @return
     */
    @Override
    public PageInfo<Type> queryType(String typeName, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<Type> types = liveDao.queryType(typeName);
        PageInfo<Type> pageInfo = new PageInfo<>(types);
        return pageInfo;
    }
    /**
     * 根据名字找分类
     * @param typeName
     * @return
     */
    @Override
    public Type queryTypeByTypeName(String typeName) {
        return liveDao.queryTypeByTypeName(typeName);
    }
    /**
     * 查询所有用户
     * @return
     */
    @Override
    public PageInfo<User> queryUser(String userName, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<User> users = liveDao.queryUser(userName);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }
    /**
     * 添加弹幕
     * @param chat
     * @return
     */
    @Override
    public boolean addChat(Chat chat) {
        //System.out.println("业务层:"+liveDao);
        
        return liveDao.addChat(chat)>0;
    }
    /**
     * 查询所有礼物
     * @return
     */
    @Override
    public List<Gift> queryGiftList() {
        return liveDao.queryGiftList();
    }

    /**
     * 送礼流程
     * @param giftId
     * @param userId
     * @return
     */
    @Override
    public GiftRecord getRecord(String giftId, String userId) {
        Gift gift = null;
        GiftRecord giftRecord =new GiftRecord();
        User user = null;
        gift = liveDao.queryGiftById(giftId);
        user = liveDao.queryUserById(userId);
        System.out.println(user.getUserPrice());
        System.out.println(gift.getGiftPrice());
        if (user.getUserPrice().compareTo(gift.getGiftPrice())  > -1){
            System.out.println(1);
            if (liveDao.updateUserPrice(gift.getGiftPrice(), userId)>0){
                giftRecord=new GiftRecord("",gift,user);
                liveDao.addGiftRecord(giftId,userId);
            }
        }
        System.out.println(giftRecord);
        return giftRecord;
    }

    /**
     * 查询所有礼物
     * @return
     */
    @Override
    public PageInfo<Gift> queryGift(String giftName, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<Gift> gifts = liveDao.queryGift(giftName);
        PageInfo<Gift> pageInfo = new PageInfo<>(gifts);
        return pageInfo;
    }

    /**
     * 添加礼物
     * @param gift
     * @return
     */
    @Override
    public boolean addGift(Gift gift) {
        return liveDao.addGift(gift)>0;
    }

    /**
     * 修改礼物
     * @param gift
     * @return
     */
    @Override
    public boolean setGift(Gift gift) {
        return liveDao.updataGift(gift)>0;
    }


    /**
     * 分页查询礼物记录表
     * @param giftRecordId
     * @param giftName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<GiftRecord> getGiftRecordList(String giftRecordId, String giftName, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<GiftRecord> giftRecords = liveDao.queryGiftRecordList(giftRecordId, giftName);
        PageInfo<GiftRecord> pageInfo = new PageInfo<>(giftRecords);
        return pageInfo;
    }

    /**
     * 删除礼物
     * @param giftId
     * @return
     */
    @Override
    public boolean deleteGift(String giftId) {
        return liveDao.deleteGift(giftId)>0;
    }

    /**
     * 这个分类下是否有视频或者直播间
     * @param typeId
     * @return
     */
    @Override
    public boolean queryTypeByTypeId(String typeId) {
        Type type = liveDao.queryTypeByTypeId(typeId);
        return type.getRoomList()==null&&type.getVideoList()==null;
    }

    /**
     * 删除
     * @param typeId
     * @return
     */
    @Override
    public boolean deleteType(String typeId) {
        return liveDao.deleteType(typeId)>0;
    }
    /**
     * 添加分类
     * @param type
     * @return
     */
    @Override
    public boolean addType(Type type) {
        return liveDao.addType(type)>0;
    }
    /**
     * 修改分类
     * @param type
     * @return
     */
    @Override
    public boolean updateType(Type type) {
        return liveDao.updateType(type)>0;
    }

    /**
     * 根据关注人数排序
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Room> queryRoomListToConcernNum(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<Room> rooms = liveDao.queryRoomListToConcernNum();
        PageInfo<Room> pageInfo = new PageInfo<>(rooms);
        return pageInfo;
    }

    /**
     * 根据用户id查询是否已是主播
     * @param userId
     * @return
     */
    @Override
    public Room queryRoomToUserId(String userId) {
        return liveDao.queryRoomToUserId(userId);
    }
}
